



********************************************************************************
* Figure 6: Placebo Test: Random FOMC Meetings
********************************************************************************

*********************************
* Generate random FOMC paths
*********************************
//Comment: Code takes significant time to run. Generate sample paths are already included

/*
//Comment: sleep (line 82 & 85) might need to be increased for some faster computers
clear all 
set maxvar 20010

use "../data/final_data_fed_secular_decline", clear
keep if date >= mdy(6,1,1989)
keep if date < mdy(7,1,2021)
keep date fomc_meet sveny10_chg

*Generate the FOMC window dummy
sort date
generate fomc_window = 0 
replace fomc_window = 1 if !missing(fomc_meet[_n+1]) | !missing(fomc_meet) | !missing(fomc_meet[_n-1])
tab fomc_window
label var fomc_window "3-day FOMC window"

*Summary statistics on the decline (FOMC) days
generate fomc_decline = fomc_window * sveny10_chg

*Compute FOMC decline since 1989
quietly summarize fomc_decline if date >= mdy(6,1,1989)
local fomc_decline_1989 = r(sum)
display "`fomc_decline_1989'"

*Number of FOMC meetings
summarize fomc_meet 
display(r(sum))
local no_days = r(sum)
display "`no_days'"

compress
save "placebopaths/placebo_paths.dta", replace

*Generate ym variable
generate ym = mofd(date)
format ym %tm
bysort ym: egen no_fomc_in_month = count(fomc_meet)

*Save file that serves as the basis for the simulation
keep date ym no_fomc_in_month sveny10_chg
save "placebopaths/placebo_original.dta", replace

*Generate 1000 sample paths
forvalues i = 1/1000 {

	display `i'
		*Loop through all months. Select no. of dates equal to no. of FOMC meetings within month
		forvalues ym = 353/737{
			use "placebopaths/placebo_original.dta", clear
			quietly display "`ym'"

			quietly keep if ym == `ym'
			local no_of_draws = no_fomc_in_month[1]
			
			*Drop if missing the 10y yield
			quietly drop if missing(sveny10_chg)
			
			drop sveny10_chg no_fomc_in_month ym
			quietly capture drop sample*
			quietly sample `no_of_draws', count
			quietly generate drawn = 1
			
			tempfile drawn_sample
			quietly save	`drawn_sample'				
				
			*Merge the drawn sample for a given ym to the main file
			use "placebopaths/placebo_paths.dta", clear
			quietly merge 1:1 date using `drawn_sample', nogen update
			sleep 1000
			quietly save "placebopaths/placebo_paths.dta", replace
		}
	sleep 1000
	use "placebopaths/placebo_paths.dta", clear
	quietly replace drawn = 0 if missing(drawn)
	quietly tab drawn 
	
	*Generate FOMC window (taking into account that some days are selected several days)
	quietly generate drawn_pre 	= drawn[_n+1] //pre-day
	quietly replace drawn_pre 	= 0 if missing(drawn_pre)
	quietly generate drawn_post = drawn[_n-1] //post-day
	quietly replace drawn_post 	= 0 if missing(drawn_post)
	quietly generate drawn_window = drawn + drawn_pre + drawn_post
	quietly drop drawn_post drawn_pre drawn

	quietly summarize drawn_window, detail
	quietly display r(sum)

	*Multiply with the yield change on a given date
	quietly generate sample`i' = drawn_window * sveny10_chg
	quietly drop drawn_window
	quietly save "placebopaths/placebo_paths.dta", replace
}

*Add-in ym variables
generate ym = mofd(date)
bysort month: egen no_fomc_in_month = count(fomc_meet)
order ym no_fomc_in_month, after(fomc_window)

save "placebopaths/placebo_paths.dta", replace

	*Erase file that is not longer needed
	erase "placebopaths/placebo_original.dta"
*/
	
*********************************
* (A) Cumulative yield decline
*********************************

use "placebopaths/placebo_paths.dta", clear

*Generate fomc decline
summarize fomc_decline
local fomc_decline_1989 = r(sum)
display ("`fomc_decline_1989'")

collapse (sum) sample*

*Reshape to long-format in order to plot the histogram
generate id = 1
reshape long sample, i(id) j(no_sample)
rename sample cum_ytm_chg
drop id

*Need to change the FOMC decline for new sample
hist cum_ytm_chg, bin(100) density ///
xtitle("Cumulative yield change (in %)") ///
xline(`fomc_decline_1989', lcolor(red) lpattern(dash)) ///
xaxis(1 2) xlabel(`fomc_decline_1989' "3d FOMC window", axis(2) labcolor(red)) ///
xtitle("", axis(2)) ///
scheme(s1mono) ylabel(, angle(horizontal))
graph export "../figures/figure6a.png", replace


*********************************
* (B) Explanatory power - Yield changes over 2 years
*********************************

use "placebopaths/placebo_paths.dta", clear

*Collapse to quarterly level
generate yq = qofd(date)
format yq %tq
collapse (sum) sveny10_chg fomc_decline sample*, by(yq)

keep if yq >= yq(1989,3) 

*Generate 2y changes (over the past)
ds yq, not
foreach z of var `r(varlist)' {
	 generate `z'_2y = `z' + `z'[_n-1] + `z'[_n-2] + `z'[_n-3] + `z'[_n-4] + `z'[_n-5] + `z'[_n-6] + `z'[_n-7] if !missing(`z'[_n-7])
} 
keep yq *_2y

*Compute residual sum of squares [Still need to sum across all time-series observations]
ds yq sveny10_chg_2y, not
foreach z of var `r(varlist)' {
	 generate `z'_rss = (sveny10_chg_2y - `z')^2
}
	
*Compute total sum of squares (tss) [Still need to sum across all time-series observations]
generate tss = (sveny10_chg_2y)^2

*Sum across all observations
collapse (sum) *_rss tss
rename sample*_2y_rss sample*
rename fomc_decline_2y_rss fomc
reshape long sample, i(fomc tss) j(no)
	
*Compute the R-squareds
generate fomc_r2 =  1 - fomc/tss
generate sample_r2 = 1 - sample/tss

*Generate label for graph
summarize fomc_r2, detail
local fomc_r2 = r(mean)
display `fomc_r2'
local fomc_r2_shift = r(mean)-0.07
	
*Generate graph
hist sample_r2, density bin(100) xtitle("R-squared") ///
xline(`fomc_r2', lcolor(red) lpattern(dash)) xaxis(1 2) ///
xlabel(`fomc_r2_shift' "3d FOMC window", axis(2) labcolor(red)) ///
xtitle("", axis(2)) scheme(s1mono)	
graph export "../figures/figure6b.png", replace